package com.androidhuman.example.Geocoder;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MyGeocoder extends Activity {
	            
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final TextView myAddress = (TextView)findViewById(R.id.myAddress);
        final EditText lat = (EditText)findViewById(R.id.latitude);
        final EditText lng = (EditText)findViewById(R.id.longitude);
        Button getAddress = (Button)findViewById(R.id.getAddress);
        
        getAddress.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Geocoder geoCoder = new Geocoder(MyGeocoder.this, Locale.KOREA);
				double latValue = Double.valueOf(lat.getText().toString());
				double lngValue = Double.valueOf(lng.getText().toString());
				
		        try{
		        	List<Address> addressList = geoCoder.getFromLocation(latValue, lngValue, 4);
		        	StringBuilder buffer = new StringBuilder();
		        	
		        	for(Address ad : addressList){
		        		buffer.append(ad.getAddressLine(0)).append("\n")
		        			.append(ad.getPostalCode()).append("\n")
		        			.append(ad.getLocality()).append("\n")
		        			.append(ad.getCountryName()).append("\n\n");
		        	}
		        	myAddress.setText(buffer.toString());
		        	
		        }catch(IOException e){
		        	e.printStackTrace();
		        }
			}
        });
    }
}